package com.myProject.control.defence;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.myProject.bean.TArea;
import com.myProject.bean.TClass;
import com.myProject.serves.AreaSev;
import com.myProject.serves.Impl.AreaSevImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "AreaServlet", urlPatterns = "/area")
public class AreaServlet extends HttpServlet {
    AreaSev as = new AreaSevImpl();

    /**
     * @param request
     * @param response
     * @return void
     * @author liuyulai
     * @date 21.4.29 10:19
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    /**
     * @param request
     * @param response
     * @return void
     * @author liuyulai
     * @date 21.4.29 10:19
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String med = request.getParameter("method");
        if (med == null || "".equalsIgnoreCase(med)) {
            showAll(request, response);
        } else if ("add".equalsIgnoreCase(med)) {
            addArea(request, response);
        }

    }

    private void addArea(HttpServletRequest request, HttpServletResponse response) {
        String areaName = request.getParameter("areaName");
        String tree = request.getParameter("tree");
        String land = request.getParameter("land");
        String treeType = request.getParameter("treeType");

        TArea tArea = new TArea();
        tArea.setTreetype(treeType);
        tArea.setLand(land);
        tArea.setTree(tree);
        tArea.setName(areaName);
        System.out.println("tArea = " + tArea);
        as.addArea(tArea);

    }

    private void showAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter pw = response.getWriter();
        int pageNo = 1;
        String Page = request.getParameter("page");
        if (Page != null) {
            pageNo = Integer.parseInt(Page);
        }
        System.out.println("pageNo = " + pageNo);
        String limit = request.getParameter("limit");
        int pageSize = Integer.parseInt(limit);
        String areaName = request.getParameter("areaName");
        String treeType = request.getParameter("treeType");
        String className = request.getParameter("className");

        TArea tArea = new TArea();
        TClass t = new TClass();
        tArea.setName(areaName);
        tArea.setTreetype(treeType);
        t.setClassName(className);
        tArea.settClass(t);

        PageInfo<TArea> show = as.showAreaByPage(pageNo, pageSize, tArea);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(show);
        System.out.println("json = " + json);
        pw.write(json);
    }
}
